package cs276.pe1.spell;

import java.util.List;

import cs276.util.Counter;
import cs276.util.StringUtils;

/**
 * Utilizes term frequency and edit distance to provide spelling corrections 
 * @author alecmgo@gmail.com
 *
 */
public class TermFrequencySpellingCorrector extends KGramSpellingCorrector {
  
  public TermFrequencySpellingCorrector() {
    
  }
  
  /**
   * Constructor accepts a special da
   * @param trainingDataFile
   */
  public TermFrequencySpellingCorrector(String trainingDataFile){
    super(trainingDataFile);
  }
  
  /**
   * Returns a list of spelling corrections
   */
  public List<String> corrections(String wordToCheck) {
    List<String> corrections = super.corrections(wordToCheck);
    
    Counter<String> rankedWords = new Counter<String>(); 
    for(String s : corrections) {
      Double rank = 1 / StringUtils.levenshtein(s, wordToCheck);
      Double probability = wordCount.getCount(s) / numTotalWords;
      
      //Weight the Edit Distance by 20, and the Term frequency by 1
      rankedWords.setCount(s, Math.pow(rank, 20) * Math.pow(probability, 1));
    }
    return rankedWords.topK(TOPK_SIZE);
  }
}
